package com.lanou3g.musicenjoy.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou3g.musicenjoy.R;
import com.lanou3g.musicenjoy.adapter.SearchingSingerAdapter;
import com.lanou3g.musicenjoy.entity.SearchSinger;
import com.lanou3g.musicenjoy.utils.NoScrollListView;
import com.lanou3g.musicenjoy.volley.VolleySingleton;

import java.util.List;


/**
 * Created by 辛鑫 on 15/10/21.
 * 歌手界面
 */
public class SearchingSingerFragment extends Fragment implements AdapterView.OnItemClickListener {

    private TextView singerSongNum;
    private RelativeLayout titleLayout;
    private NoScrollListView singerList;

    private String newUrl;
    private List<SearchSinger.DataEntity> data;
    private RequestQueue requestQueue;
    private SearchingSingerAdapter adapter;


    public static SearchingSingerFragment passData(Context mContext, String str) {
        SearchingSingerFragment fragment = new SearchingSingerFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url", str);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.searching_singer_layout, null);
        singerSongNum = (TextView) view.findViewById(R.id.singer_song_num);
        titleLayout = (RelativeLayout) view.findViewById(R.id.title_layout);
        singerList = (NoScrollListView) view.findViewById(R.id.singer_list);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        singerList.setOnItemClickListener(this);
        adapter = new SearchingSingerAdapter(getActivity());
        requestQueue = VolleySingleton.getInstance(getContext()).getRequestQueue();
        Bundle bundle = getArguments();
        String url = bundle.getString("url");
        newUrl = "http://so.ard.iyyin.com/s/artist?uid=866058021842951&f=f3040&app=ttpod&hid=1135212123029932&alf=alf203200&net=2&size=50&v=v8.2.0.2015091720&utdid=VSeGyjss0fEDAOqIktP5W%2BO4&s=s200&page=1&q=" + url + "&imsi=460078098497828&tid=0";
        Log.i("searching_url", newUrl + "0000");
        getInternetData();
    }



    //从网络上拉取数据
    private void getInternetData() {
        StringRequest request = new StringRequest(newUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Gson gson = new Gson();
                SearchSinger result = gson.fromJson(s, SearchSinger.class);
                Log.i("xin", result + "");
                if (result != null && result.getData() != null) {
                    data = result.getData();
                    Log.i("xin", data + "");
                    adapter.setDatas(data);
                    singerList.setAdapter(adapter);
                    singerSongNum.setText(data.size() + "");
                    titleLayout.setVisibility(View.VISIBLE);
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        requestQueue.add(request);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SearchSinger.DataEntity entity = (SearchSinger.DataEntity) parent.getItemAtPosition(position);
        int urlId = entity.get_id();
        SingerContentFragment fragment = new SingerContentFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("id", urlId);
        fragment.setArguments(bundle);
        getFragmentManager()
                .beginTransaction()
                .addToBackStack(null)
                .replace(R.id.container, fragment)
                .commit();

    }
}
